package com.blindBox.front.handler.verify.loginVerifyImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blindBox.front.commonResult.MsgBackToFront;
import com.blindBox.front.dao_pojo.User;
import com.blindBox.front.service.UserService;
import com.blindBox.front.service.impl.UserServiceImpl;
import com.blindBox.front.token.TokenTool;
import com.blindBox.front.handler.verify.Verify;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;

//检验令牌登录的类
@Component("uTokenVerify")
public class UserLoginByTokenVerify implements Verify<MsgBackToFront>
{
    @Autowired
    private TokenTool tokenTool;
    @Autowired
    private UserService userService;
    //jackson
    private  volatile ObjectMapper objectMapper = new ObjectMapper();

    //检验令牌有效性的方法
    @Override
    public MsgBackToFront verify(Object obj)
    {
       return doVerify((String) obj);
    }

    //在拦截器中使用该方法进行token验证,拦截器中无法注入tokentool
    public MsgBackToFront verify(String token,TokenTool tt)
    {
        if(tokenTool==null)
        {
            this.tokenTool=tt;
        }
        return doVerify(token);
    }

    //真正来检验令牌的方法
    public MsgBackToFront doVerify(String token)
    {
        try {
            String uJsonString = tokenTool.parse(token);
            //令牌解析错误
            if(uJsonString==null)
                return new MsgBackToFront(null,MsgBackToFront.TOKEN_ERROR);
            //判断令牌是否过期
            if(isExpired(token))
                return new MsgBackToFront(null,MsgBackToFront.TOKEN_ERROR);
            //将json字符串转化为user对象
            //正确
            User user = objectMapper.readValue(uJsonString, User.class);
            //如果转换失败，即token存在问题
            if(user==null)
            return new MsgBackToFront(null,MsgBackToFront.TOKEN_ERROR);
            //判断用户是否被封号
            if(isBanned(user))
            return new MsgBackToFront(null,MsgBackToFront.USER_NO_RIGHT);
            //验证通过，返回正确的数据
            return  new MsgBackToFront(user,MsgBackToFront.HANDLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //出现异常
        return new MsgBackToFront(null,MsgBackToFront.EXCEPTION_ERROR);
    }

    //检验令牌是否过期
    private boolean isExpired(String token)
    {
        //判断令牌是否过期
        Calendar time = tokenTool.getExpiredTime(token);
        //获取到过期的时间，与当前时间对比，判断是否已经过期了
        long expiredTime = time.getTimeInMillis();
        long nowTime = Calendar.getInstance().getTimeInMillis();
        if (nowTime>expiredTime)
            //过期了
            return true;
        return false;
    }

    //验证用户是否封号
    private  boolean isBanned(User user)
    {
        //如果是拦截器，就不进行封号检测
        //userMapper不好注入
        if(userService==null)
        {
            return true;
        }
        User ret= userService.getOne(new QueryWrapper<User>().eq("uname", user.getUname()).eq("upwd", user.getUpwd()));
        //被封号了
        if(ret.getFlag()==1)
            return true;
        return false;
    }
}
